<template>
    <div class="top">
        <div class="left">
            <el-button size="small" @click="handleCollapse">
                <component class="icons" :is="'menu'" />
            </el-button>
            <el-breadcrumb separator="/" class="bread">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="right">
            <el-dropdown>
                <span class="el-dropdown-link">
                    <img class="user" :src="userInfo.avatar" alt="">
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item disabled>{{ userInfo.name }}</el-dropdown-item>
                        <el-dropdown-item @click="logout">退出</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </div>
    </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useStore } from '@/store';
import { useRouter } from 'vue-router'
import { getUserInfo } from '@/api/user';
const router = useRouter()

const store = useStore()
const handleCollapse = () => {
    store.state.isCollapse = !store.state.isCollapse
}
const logout = async () => {
    router.push('/login')
    store.removeToken()
}
const userInfo = ref({})

onMounted(() => {
    if (store.token) {
        getUserInfo(store.token).then(res => {
            if (res.code === 200) {
                userInfo.value = {
                    id: res.data.id,
                    status: res.data.status,
                    avatar: res.data.avatar,
                    name: res.data.name,
                    ipInfo: res.data.ipInfo,
                }
            }
        })
    }
})
</script>


<style lang="less" scoped>
.top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 100%;
    background-color: #333;
}

.icons {
    width: 20px;
    height: 20px;
}

.left {
    display: flex;
    align-items: center;

    .el-button {
        margin-right: 20px;
    }
}

.right {
    .user {
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }
}

:deep(.bread span) {
    color: #fff !important;
    cursor: pointer !important;
}
</style>